package com.formula.datastructure.standard.list;

public class LinkedList {
    // java没有指针概念, 所以如果head本身不存放内容，就是头结点
    private LinkedNode head;
    // 如果要使用尾插法, 就用tail
    private LinkedNode tail;

    private int size;

    public void add(Integer input) {
        LinkedNode newNode = new LinkedNode(input);
        if (head != null) {
            newNode.next = head.next;
        }
        head = newNode;
        size++;
    }

    public void delete(Integer input) {
        // 不带头结点就会多判断
        LinkedNode temp = head;
        if (head.data == input) {
            head = head.next;
            size--;
            return;
        } else {
            while (temp.next != null) {
                if (temp.next.data == input) {
                    break;
                }
                temp = temp.next;
            }
            if (temp == null) {
                System.out.println("No such element.");
            } else {
                temp.next = temp.next.next;
                size--;
            }
        }
    }
}
